/*

This file is part of roomyak.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
roomyak is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
roomyak is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with roomyak; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.apache.commons.lang.StringUtils;

public class StringHelper extends StringUtils
{
	public static final String NEW_LINE;
	
	private static final int KILO = 1024;
	private static final int MEGA = KILO * KILO;
	private static final int GIGA = MEGA * KILO;
	private static NumberFormat FILE_SIZE_FORMAT = new DecimalFormat("####.###");
	
	static
	{
		NEW_LINE = System.getProperty("line.separator");
	}
	
	public static String formatByteSize(double size)
	{
		String prefix = "";
		if (size > GIGA)
		{
			prefix = "Gb";
			size /= GIGA;
		}
		else if (size > MEGA)
		{
			prefix = "Mb";
			size /= MEGA;
		}
		else if (size > KILO)
		{
			prefix = "Kb";
			size /= KILO;
		}
		
		String s = FILE_SIZE_FORMAT.format(size);
		if (s.length() > 4)
			s = s.substring(0, s.charAt(3)=='.'?3:4);
		return s  + ' ' + prefix;
//		String s = String.valueOf(size);
//		int index = s.indexOf('.');
//		if (s.length() > index + 2)
//			s = s.substring(0, index + 2);
//		
//		return s + ' ' + prefix + 'b';
	}
	
	public static boolean getConventionalBoolean(String value)
	{
		if(StringHelper.isEmpty(value))
		{
			return false;
		}
		
		value = value.trim().toLowerCase();
		
		if(value.equals("true") || value.equals("yes") || value.equals("1") || value.equals("ok"))
		{
			return true;
		}
		
		return false;
	}
	
}
